<?php

class SF_AutoCoupon_Helper_Data extends Mage_Core_Helper_Abstract
{
    /**
     * $rule_id 规则id
     * $count  生成coupon数量
     * $prefix  前缀
     * $suffix  后缀
     * $format 生成格式
     * $dash_every_x_characters 多少个字符分割一次
     * $length  本身长度
     */
    public function getCouponCode($rule_id,$record,$prams=array()){
        // Get the rule in question
        $rule = Mage::getModel('salesrule/rule')->load($rule_id); //21 = ID of coupon in question
                                                                                                                                                                                                                                            
        // Define a coupon code generator model instance
        // Look at Mage_SalesRule_Model_Coupon_Massgenerator for options
         
        $generator = Mage::getModel('salesrule/coupon_massgenerator');
        $parameters = array(
            'count'=>$prams['count'],
            'format'=>$prams['format'],
            'dash_every_x_characters'=>$prams['dash_every_x_characters'],
            'prefix'=>$prams['prefix'],
            'suffix'=>$prams['suffix'],
            'length'=>$prams['length'],
        );
         
        if( !empty($parameters['format']) ){
            switch( strtolower($parameters['format']) ){
                case 'alphanumeric':
                case 'alphanum':
                    $generator->setFormat( Mage_SalesRule_Helper_Coupon::COUPON_FORMAT_ALPHANUMERIC );
                    break;
                case 'alphabetical':
                case 'alpha':
                    $generator->setFormat( Mage_SalesRule_Helper_Coupon::COUPON_FORMAT_ALPHABETICAL );
                    break;
                case 'numeric':
                case 'num':
                    $generator->setFormat( Mage_SalesRule_Helper_Coupon::COUPON_FORMAT_NUMERIC );
                    break;
            }
        }else{
            $generator->setFormat( Mage_SalesRule_Helper_Coupon::COUPON_FORMAT_ALPHABETICAL );
        }     
        $generator->setDash( !empty($parameters['dash_every_x_characters'])? (int) $parameters['dash_every_x_characters'] : 0);
        $generator->setLength( !empty($parameters['length'])? (int) $parameters['length'] : 8);
        $generator->setPrefix( !empty($parameters['prefix'])? $parameters['prefix'] : '');
        $generator->setSuffix( !empty($parameters['suffix'])? $parameters['suffix'] : '');
         
        // Set the generator, and coupon type so it's able to generate
        $rule->setCouponCodeGenerator($generator);
        $rule->setCouponType( Mage_SalesRule_Model_Rule::COUPON_TYPE_AUTO );
         
        // Get as many coupons as you required
        $count = !empty($parameters['count'])? (int) $parameters['count'] : 1;
        $codes = array();
        for( $i = 0; $i < $count; $i++ ){
            $coupon = $rule->acquireCoupon();
            $coupon
            ->setType(Mage_SalesRule_Helper_Coupon::COUPON_TYPE_SPECIFIC_AUTOGENERATED)
            ->save();
            if(is_array($record)&&isset($record['email'])){
                $couponLog = Mage::getModel("autocoupon/couponlog");
                $couponLog->setCouponId($coupon->getCouponId())
                ->setEmail($record['email']?$record['email']:'')
                ->setEventsCode($record['events_code']?$record['events_code']:0)
                ->save();
            }
 
            $code = $coupon->getCode();
            $codes['code'][] = $code;
        }
           $codes['description']= $rule->getDescription();
           $codes['amount'] = $rule->getDiscountAmount();
           $codes['name']= $rule->getName();
            return $codes;
    }
	 
}
